/**
 * 		AbstractMochitoTransferableObject.java
 *				------------
 *		
 *   Author:	bin04017@gmail.com
 *	 Created:	22.12.2006
 *	 Project:	Mochito-TransferFramework
 *	 Package:	abstractMochitoTranferableObject
 *	 Usage:		abstract base transfer class for mochito system
 * *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */
package mochitoTransferableObject;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.Random;

import mochitoTransferFactory.MochitoException;

/**
 * Abstract AbstractMochitoTransferableObject Class Use: best use is with std
 * Constructor else you risk some heavy troubles
 * 
 * @extends UnicastRemoteObject
 * @author bin04017@gmail.com
 */
public abstract class AbstractMochitoTransferableObject implements Serializable {
	/**
	 * Auto generated ID
	 */
	private static final long serialVersionUID = -5231457836031005148L;

	/**
	 * the MochitoTransferableObjectId id of this object
	 */
	private Long MochitoTransferableObjectId = -1L;

	/**
	 * the MochitoTransferableObjectType Type of Object
	 */
	private Integer MochitoTransferableObjectType = -1;

	/**
	 * the MochitoTransferableObjectSender Sender of this object
	 */
	private String MochitoTransferableObjectSender = null;

	/**
	 * the MochitoTransferableObjectData Data of this object
	 */
	private Byte[] MochitoTransferableObjectData = null;

	/**
	 * Consturtor with 
	 * "auto" generated id(IP without points + random number(int)) else String input
	 * 
	 * @param id
	 * @throws RemoteException
	 * @throws RemoteException
	 * @throws UnknownHostException
	 * @throws MochitoException 
	 * @throws RemoteException 
	 * @throws
	 */
	public AbstractMochitoTransferableObject(String init) throws MochitoException, RemoteException {
		super();
		if(init.equalsIgnoreCase("auto")){
			this.MochitoTransferableObjectId = generateId();
		} else {
			this.MochitoTransferableObjectId = Long.parseLong(init);
		}
	}
	/**
	 * Constructor for normal use
	 * @throws MochitoException
	 * @throws RemoteException
	 */
	public AbstractMochitoTransferableObject() throws MochitoException, RemoteException{
		super();
		this.MochitoTransferableObjectId = generateId();
	}
	/**
	 * Consturctor with user generated id
	 * 
	 * @param id
	 * @throws RemoteException
	 */
	public AbstractMochitoTransferableObject(Long id) throws RemoteException {
		super();
		this.MochitoTransferableObjectId = id;
	}

	/**
	 * generate a random id from ip and a random integer
	 * 
	 * @return
	 * @throws MochitoException 
	 * @throws UnknownHostException
	 */
	private Long generateId() throws MochitoException {
		Random r = new Random();
		Integer randint = r.nextInt(9999);
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			throw new MochitoException(e);
		}
		String ipAsString = addr.getHostAddress();
		String [] ipArray = null;
		ipAsString = ipAsString.replace(".", "/");
		ipArray = ipAsString.split("/");
		ipAsString = ipArray[0] + ipArray[1] + ipArray[2] + ipArray[3];
		Long id = Long.parseLong(ipAsString + randint.toString());
		return id;
	}

	/**
	 * 
	 * @return the MochitoTransferableObjectId
	 */
	public Long getMochitoTransferableObjectId() {
		return MochitoTransferableObjectId;
	}

	/**
	 * @return the mochitoTransferableObjectType
	 */
	public Integer getMochitoTransferableObjectType() {
		return MochitoTransferableObjectType;
	}

	/**
	 * @param mochitoTransferableObjectType
	 *            the mochitoTransferableObjectType to set
	 */
	public void setMochitoTransferableObjectType(
			Integer mochitoTransferableObjectType) {
		MochitoTransferableObjectType = mochitoTransferableObjectType;
	}

	/**
	 * @return the mochitoTransferableObjectSender
	 */
	public String getMochitoTransferableObjectSender() {
		return MochitoTransferableObjectSender;
	}

	/**
	 * @param mochitoTransferableObjectSender
	 *            the mochitoTransferableObjectSender to set
	 */
	public void setMochitoTransferableObjectSender(
			String mochitoTransferableObjectSender) {
		MochitoTransferableObjectSender = mochitoTransferableObjectSender;
	}

	/**
	 * @param mochitoTransferableObjectId the mochitoTransferableObjectId to set
	 */
	public void setMochitoTransferableObjectId(Long mochitoTransferableObjectId) {
		MochitoTransferableObjectId = mochitoTransferableObjectId;
	}

	/**
	 * @return the mochitoTransferableObjectData
	 */
	public Byte[] getMochitoTransferableObjectData() {
		return MochitoTransferableObjectData;
	}

	/**
	 * @param mochitoTransferableObjectData
	 *            the mochitoTransferableObjectData to set
	 */
	public void setMochitoTransferableObjectData(
			Byte[] mochitoTransferableObjectData) {
		MochitoTransferableObjectData = mochitoTransferableObjectData;
	}
	
	public MochitoTransferableInfo getInfo(){
		MochitoTransferableInfo info = new MochitoTransferableInfo();
		info.setMochitoTransferableObjectId(this.getMochitoTransferableObjectId());
		info.setMochitoTransferableObjectSender(this.getMochitoTransferableObjectSender());
		info.setMochitoTransferableObjectType(this.getMochitoTransferableObjectType());
		return info;
	}
}
